package com.koron.order.linedefect.defectPending.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.order.common.tools.CommonCodeTools;
import com.koron.order.common.tools.OrderCodeEnum;
import com.koron.order.defect.bean.po.DefectStatus;
import com.koron.order.linedefect.base.bean.dto.DefectDTO;
import com.koron.order.linedefect.base.bean.po.DefectBean;
import com.koron.order.linedefect.base.service.DefectService;
import com.koron.order.linedefect.defectPending.bean.po.DefectPendingPo;
import com.koron.order.linedefect.defectPending.bean.po.DefectPendingStatus;
import com.koron.order.linedefect.defectPending.bean.query.DefectPendingQuery;
import com.koron.order.linedefect.defectPending.bean.vo.DefectPendingVo;
import com.koron.order.linedefect.defectPending.mapper.DefectPendingMapper;
import com.koron.order.linedefect.defectPending.service.DefectPendingService;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 缺陷工单下缺陷待处理信息从表(DefectPending)表服务实现类
 *
 * @author zhouj
 * @since 2022-02-09 10:36:18
 */
@Service("lineDefectPendingService")
public class DefectPendingServiceImpl implements DefectPendingService {

    @Autowired
    private DefectService lineDefectService;

    @Autowired
    private WorkFlowHandler workFlowHandler;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public DefectPendingPo insertOrUpdate(SessionFactory factory, DefectPendingPo defectPending) {

        DefectPendingMapper mapper = factory.getMapper(DefectPendingMapper.class);

        if (StringUtils.isEmpty(defectPending.getId())) {
            DefectBean defectBean = lineDefectService.queryById(factory, defectPending.getDefectId());
            defectPending.setCode(CommonCodeTools.getNormalOrderCode(defectBean.getCode(), OrderCodeEnum.DCL, 2));
            defectPending.setStatus(DefectPendingStatus.DRAFT.getStatus());
        }

        mapper.insertOrUpdate(defectPending);

        return defectPending;
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<DefectPendingPo> defectPendingList) {

        DefectPendingMapper mapper = factory.getMapper(DefectPendingMapper.class);

        return mapper.insertOrUpdateBatch(defectPendingList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, DefectPendingPo defectPending) {

        DefectPendingMapper mapper = factory.getMapper(DefectPendingMapper.class);

        return mapper.insert(defectPending);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<DefectPendingPo> defectPendingList) {

        DefectPendingMapper mapper = factory.getMapper(DefectPendingMapper.class);

        return mapper.insertBatch(defectPendingList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, DefectPendingPo defectPending) {
        DefectPendingMapper mapper = factory.getMapper(DefectPendingMapper.class);
        return mapper.update(defectPending);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        DefectPendingMapper mapper = factory.getMapper(DefectPendingMapper.class);
        int i = mapper.deleteById(id);
        workFlowHandler.delete(id);
        return i;
    }

    @Override
    @TaskAnnotation("queryById")
    public DefectPendingPo queryById(SessionFactory factory, String id) {

        DefectPendingMapper mapper = factory.getMapper(DefectPendingMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<DefectPendingPo> queryList(SessionFactory factory, DefectPendingQuery defectPendingQuery) {

        DefectPendingMapper mapper = factory.getMapper(DefectPendingMapper.class);

        List<DefectPendingPo> list = mapper.queryList(defectPendingQuery);

        return list;
    }

    @Override
    @TaskAnnotation("queryVoPageList")
    public PageInfo queryVoPageList(SessionFactory factory, DefectPendingQuery pendingQuery, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy(false));
        DefectPendingMapper mapper = factory.getMapper(DefectPendingMapper.class);

        List<DefectPendingVo> list = mapper.queryVoPageList(pendingQuery);

        return new PageInfo(list);
    }

    /*
     * 功能描述 提交工单状态接口
     * @author zhouj
     * @date 2021/12/21 10:55
     * @param [factory, orderId:工单id, status:将要改变的状态]
     * description 对于状态的修改需要进行顺序的判断,只能往后流转,
     * @return int
     */
    @Override
    @TaskAnnotation("submitDefectPending")
    public int submitDefectPending(SessionFactory factory, String orderId, String status) {

        DefectPendingMapper mapper = factory.getMapper(DefectPendingMapper.class);
        //根据工单id 先判断工单当时的状态
        DefectPendingPo preDefectPendingPo = mapper.queryById(orderId);
        String preStatus = preDefectPendingPo.getStatus();

        //将要改变的状态
        DefectPendingStatus defectPendingStatus = DefectPendingStatus.getDefectPendingStatus(status);
        Assert.notNull(defectPendingStatus, "缺陷降级状态修改,枚举不能为空");

        if (StringUtils.isNotEmpty(preStatus)) {
            DefectPendingStatus preDefectPendingStatus = DefectPendingStatus.getDefectPendingStatus(preStatus);
            Assert.notNull(preDefectPendingStatus, "缺陷降级数据有误");
            Assert.isTrue(defectPendingStatus.getSort() - preDefectPendingStatus.getSort() == 1, "不可重复提交");
        } else {
            Assert.isTrue(StringUtils.equalsAny(defectPendingStatus.getStatus(),
                    DefectPendingStatus.DRAFT.getStatus(),
                    DefectPendingStatus.WAIT_REVIEW.getStatus()), "修改缺陷降级工单状态失败");
        }
        DefectPendingPo po = new DefectPendingPo();
        po.setId(orderId);
        po.setStatus(status);

        if (StringUtils.equalsIgnoreCase(status, DefectPendingStatus.REVIEWED.getStatus())) {

            DefectBean defectBean = lineDefectService.queryById(factory, preDefectPendingPo.getDefectId());

            // 如果是审核完成,即改为已审批时,需要更改缺陷工单的预计完成时间 还有缺陷工单的状态改为待处理,缺陷工单挂起(方便后面激活)
            DefectDTO defectDTO = new DefectDTO();
            defectDTO.setId(preDefectPendingPo.getDefectId());
            defectDTO.setExpectProcessTime(preDefectPendingPo.getExpectProcessTime());
            defectDTO.setStatus(DefectStatus.PENGDING.getStatus());
            defectDTO.setLevel(defectBean.getLevel());
//            lineDefectService.saveOrUpdate(factory, defectDTO);
            // todo 修改对应缺陷单的计划完成时间和应完成处理时间
            lineDefectService.updateDefect(factory, defectDTO);
            po.setDefectStatus(defectBean.getStatus());
        }

        return mapper.update(po);
    }

}
